package com.shiningstars.parse;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.shiningstars.general.Constants;
import com.shiningstars.general.Utilities;
import com.shiningstars.service.AdminFeedbackService;
import com.shiningstars.service.AuditService;
import com.shiningstars.service.ErrorService;
import com.shiningstars.service.FeedbackNotifyService;
import com.shiningstars.service.FeedbackSendService;
import com.shiningstars.service.MSLocatorService;
import com.shiningstars.service.ParseService;
import com.shiningstars.service.StatusService;
import com.shiningstars.service.UpdateService;

public class ServiceStartReceiver extends BroadcastReceiver implements Constants {

	@Override
	public void onReceive(Context context, Intent intent) {

		try {
			String role = Utilities.getSharedPreference(context, ROLE);

			JSONObject json = new JSONObject(intent.getExtras().getString(PARSE_EXTRA_DATA_KEY));
			String serviceName = json.getString(SERVICE_NAME);

			if (ALL.equals(serviceName)) {
				if (!Utilities.isRunning(context, StatusService.class.getName())) {
					context.startService(new Intent(context, StatusService.class));
				}
				if (!Utilities.isRunning(context, AuditService.class.getName())) {
					context.startService(new Intent(context, AuditService.class));
				}
				if (!Utilities.isRunning(context, ErrorService.class.getName())) {
					context.startService(new Intent(context, ErrorService.class));
				}
				if (!Utilities.isRunning(context, FeedbackSendService.class.getName())) {
					context.startService(new Intent(context, FeedbackSendService.class));
				}
				if (!Utilities.isRunning(context, FeedbackNotifyService.class.getName())) {
					context.startService(new Intent(context, FeedbackNotifyService.class));
				}
				if (!Utilities.isRunning(context, MSLocatorService.class.getName())) {
					context.startService(new Intent(context, MSLocatorService.class));
				}
				if (!Utilities.isRunning(context, ParseService.class.getName())) {
					context.startService(new Intent(context, ParseService.class));
				}
				if (!Utilities.isRunning(context, UpdateService.class.getName())) {
					context.startService(new Intent(context, UpdateService.class));
				}
				if (ADMIN.equals(role) && !Utilities.isRunning(context, AdminFeedbackService.class.getName())) {
					context.startService(new Intent(context, AdminFeedbackService.class));
				}
			} else if (STATUS_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, StatusService.class.getName())) {
					context.startService(new Intent(context, StatusService.class));
				}
			} else if (AUDIT_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, AuditService.class.getName())) {
					context.startService(new Intent(context, AuditService.class));
				}
			} else if (ERROR_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, ErrorService.class.getName())) {
					context.startService(new Intent(context, ErrorService.class));
				}
			} else if (FEEDBACK_SEND_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, FeedbackSendService.class.getName())) {
					context.startService(new Intent(context, FeedbackSendService.class));
				}
			} else if (FEEDBACK_NOTIFY_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, FeedbackNotifyService.class.getName())) {
					context.startService(new Intent(context, FeedbackNotifyService.class));
				}
			} else if (MS_LOCATOR_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, MSLocatorService.class.getName())) {
					context.startService(new Intent(context, MSLocatorService.class));
				}
			} else if (PARSE_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, ParseService.class.getName())) {
					context.startService(new Intent(context, ParseService.class));
				}
			} else if (UPDATE_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, UpdateService.class.getName())) {
					context.startService(new Intent(context, UpdateService.class));
				}
			} else if (ADMIN.equals(role) && ADMIN_FEEDBACK_SERVICE.equals(serviceName)) {
				if (!Utilities.isRunning(context, AdminFeedbackService.class.getName())) {
					context.startService(new Intent(context, AdminFeedbackService.class));
				}
			}
		} catch (JSONException e) {
			Utilities.logError(context, ServiceStartReceiver.class.getName(), e);
		}
	}
}